package tech.aistar.wyymusicplayer.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tech.aistar.wyymusicplayer.mapper.UserMapper;
import tech.aistar.wyymusicplayer.pojo.User;

@Service
public class UserServiceImpl implements UserService {
    //service层需要调用mapper层
    @Autowired
    private UserMapper userMapper;

    @Override
    public User findByEmail(String email) {
        //1. 调用mapper层的根据邮箱查找用户,如果找不到返回null
        User user = userMapper.getByEmail(email);
        return user;
    }

    @Override
    public boolean registerUser(String username) {
        return userMapper.InsertUser(username);
    }

    @Override
    public Boolean InsertMail(String email) {
        return userMapper.InsertMail(email,1);
    }

    @Override
    public boolean isDefualtUsername(String username) {
        return userMapper.isDefaultUsername(username);
    }
    // 更新用户名
    public boolean updateUsername(Integer userId, String newUsername) {
        return userMapper.updateUsername(userId, newUsername) > 0;
    }

    @Override
    public boolean UpdateisDefaultUsername(Integer userId) {
        return userMapper.UpdataIsDefaultUsername(0,userId);
    }

    @Override
    public User GetById(Integer id) {
        return userMapper.getById(id);
    }

    // 检查用户名是否存在
    public boolean usernameExists(String username) {
        return userMapper.existsByUsername(username);
    }

}